<template>
  <div id="recommendView">
    <a-row class="grid-demo">
      <a-col :xs="0" :sm="0" :md="0" :lg="1" :xl="2" :xxl="1"></a-col>
      <a-col :xs="24" :sm="24" :md="24" :lg="22" :xl="20" :xxl="22">
        <a-list
          class="list-demo-action-layout"
          :bordered="false"
          :data="dataList"
          :pagination-props="paginationProps"
          @page-change="onPageChange"
        >
          <template #item="{ item }">
            <a-list-item
              class="list-demo-item"
              action-layout="vertical"
              @click="goDetails(item.id)"
            >
              <template #actions>
                <span><icon-heart />0</span>
                <span><icon-star />0</span>
                <span><icon-message />评论</span>
              </template>
              <template #extra>
                <div class="image-area">
                  <img alt="arco-design" :src="item.imageSrc" />
                </div>
              </template>
              <a-list-item-meta :title="item.title">
                <template #avatar>
                  <a-avatar shape="square">
                    <img
                      alt="avatar"
                      :src="
                        item.userVO.userAvatar == null
                          ? 'http://hoj.h1234.top/img/logo.b5581132.svg'
                          : item.userVO.userAvatar
                      "
                    />
                  </a-avatar>
                </template>
              </a-list-item-meta>
              <a-list-item-meta :description="item.content" class="limitWord">
              </a-list-item-meta>
            </a-list-item>
          </template>
        </a-list>
      </a-col>
      <a-col :xs="0" :sm="0" :md="0" :lg="1" :xl="2" :xxl="1"></a-col>
    </a-row>
  </div>
</template>

<script setup lang="ts">
import { useRouter } from "vue-router";
import { reactive, ref, watchEffect } from "vue";
import { ArticleControllerService } from "../../../generated";
import { Message } from "@arco-design/web-vue";

const router = useRouter();

// 数据列表
const dataList = ref([]);

// 分页参数
const pageParams = ref({
  current: 1,
  pageSize: 3,
});

// 分页结果参数
let paginationProps = reactive({
  showTotal: true,
  current: 0,
  pageSize: 0,
  total: 0,
});

/**
 * 页码改变时触发
 */
const onPageChange = (page: number) => {
  pageParams.value = {
    ...pageParams.value,
    current: page,
  };
};

// 加载数据
const loadData = async () => {
  const res = await ArticleControllerService.listRecommendByPageUsingPost(
    pageParams.value as any
  );
  if (res.code === 0) {
    // 数据赋值
    dataList.value = res.data.records;
    // 分页结果参数赋值
    paginationProps.total = Number(res.data.total);
    paginationProps.current = Number(res.data.current);
    paginationProps.pageSize = Number(res.data.size);
  } else {
    Message.error("获取文章失败，" + res.message);
  }
};

/**
 * 去文章详情页
 * @param articleId 文章id
 */
const goDetails = (articleId: number) => {
  router.push(`/article/detail/${articleId}`);
};

watchEffect(() => {
  loadData();
});
</script>

<style scoped>
/*预览内容限制字数*/
.limitWord {
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
}

.list-demo-action-layout .image-area {
  width: 183px;
  height: 119px;
  border-radius: 2px;
  overflow: hidden;
}

.list-demo-action-layout .list-demo-item {
  padding: 20px 0;
  border-bottom: 1px solid var(--color-fill-3);
  cursor: pointer;
}

.list-demo-action-layout .image-area img {
  width: 100%;
}

.list-demo-action-layout .arco-list-item-action .arco-icon {
  margin: 0 4px;
}
</style>